//
//  HJStatusOriginalView.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/11.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit
import SnapKit

class HJStatusOriginalView: UIView {
    
    var bottomConstraint : Constraint?
    var statusViewModel:HJStatusViewModel?{
        
        didSet{
            
            iconView.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLabel.text = statusViewModel?.createAtText
            sourceLabel.text = statusViewModel?.sourceTxet
            contentLabel.text = statusViewModel?.status?.text
            vipImageView.image = statusViewModel?.vipImage
            verifiedImageView.image = statusViewModel?.verrified
            
            
            self.bottomConstraint?.uninstall()
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
                pictureView.pic_urls = pic_urls
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(HJStatusCellMargin).constraint
                     
                })
            }else{
                
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(HJStatusCellMargin).constraint
                    
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        
        
        
        //添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HJStatusCellMargin)
            make.leading.equalTo(self).offset(HJStatusCellMargin)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        // 昵称的label
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(HJStatusCellMargin)
        }
        // 会员图标
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(HJStatusCellMargin)
        }
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(HJStatusCellMargin)
        }
        // 认证
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        // 原创微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            //            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(HJStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HJStatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(HJStatusCellMargin).constraint
        }

    }
    
    // MARK: - 懒加载控件
    // 头像控件
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    // 昵称label
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HJStatusNameFontSize)
    // 时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: HJStatusTimeFontSize)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: HJStatusTimeFontSize)
    // 会员图标
    private lazy var vipImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    // 认证
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    // 微博内容
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HJStatusContentFontSize, maxWidth: SCREENW - 2 * HJStatusCellMargin)
    private lazy var pictureView: HJStatusPictureView = {
        
        let view = HJStatusPictureView()
        //view.backgroundColor = UIColor(white: 0.95, alpha: 1)
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
}

